package com.itheima.controller;

import com.itheima.pojo.Account;
import com.itheima.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.nio.channels.AcceptPendingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/getName")
    public String getName(@RequestParam("name") String username, int age) {
        System.out.println("用户名为:" + username + ",年龄为:" + age);
        return "success";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(User user) {
        System.out.println(user);
        return "success";
    }

    @RequestMapping("/deleteAll")
    public String deleteAll(@RequestParam List<Integer> ids) {
        System.out.println(ids);
        return "success";
    }

    @RequestMapping("/paramsToMap")
    public String paramsToMap(@RequestParam Map<String, Object> map) {
        System.out.println(map);
        return "success";
    }

    @RequestMapping("/useServletApi")
    public String useServletApi(HttpSession session, HttpServletRequest request) {
        System.out.println(request.getHeader("User-Agent"));
        session.setAttribute("username", "张三");
        return "success";
    }

    @RequestMapping(value = "/getRequestBody", method = RequestMethod.POST)
    public String getRequestBody(@RequestBody User user) {
        System.out.println(user);
        return "success";
    }

    @DeleteMapping("/{id}")
    public String deleteById(@PathVariable("id") int id) {
        System.out.println("要删除的用户id为:" + id);
        return "success";
    }

    @GetMapping("/{id}")
    public String findById(@PathVariable("id") int id) {
        System.out.println("要查询的用户id为:" + id);
        return "success";
    }

    @RequestMapping("/getUserAgent")
    public String getUserAgent(@RequestHeader("User-Agent") String userAgent) {
        System.out.println("获取的请求头的信息为:" + userAgent);
        return "success";
    }

    @ModelAttribute
    public void setCookie(HttpServletResponse response) {
        Cookie cookie = new Cookie("username", "lcslove");
        cookie.setMaxAge(60 * 60);
        response.addCookie(cookie);
    }

    @RequestMapping("/getCookieValue")
    public String getCookieValue(@CookieValue("username") String cookieValue) {
        System.out.println("获取cookie的值为:" + cookieValue);
        return "success";
    }

    /*@ModelAttribute
    public Account setAccountName(int id) {
        Account account = new Account();
        account.setName("张三");
        return account;
    }*/

    @RequestMapping(value = "/updateAccount", method = RequestMethod.POST)
    public String updateAccount(Account account) {
        System.out.println("修改账户信息之后为:" + account);
        return "success";
    }

    @RequestMapping("/useModelAndView")
    public ModelAndView useModelAndView() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg", "我是你爸爸");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/responseJson")
    public User responseJson() {
        List<String> list = new ArrayList<>();
        list.add("basketball");
        list.add("football");
        list.add("tennisball");
        list.add("volleyball");
        User user = new User();
        user.setName("李二狗");
        user.setAge(25);
        user.setAddress("火星");
        user.setBirthday(new Date());
        user.setHobby(list);
        return user;
    }
}
